home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / sh03src.zoo / sh-pl03 / sh / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-22  |  8.8 KB  |  403 lines

  1. /*-
  2.  * Copyright (c) 1991 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Kenneth Almquist.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)input.c    5.4 (Berkeley) 7/1/91";
  39. #endif /* not lint */
  40.  
  41. /*
  42.  * This file implements the input routines used by the parser.
  43.  */
  44.  
  45. #include <stdio.h>    /* defines BUFSIZ */
  46. #include "shell.h"
  47. #include <fcntl.h>
  48. #include <errno.h>
  49. #include "syntax.h"
  50. #include "input.h"
  51. #include "output.h"
  52. #include "memalloc.h"
  53. #include "error.h"
  54.  
  55. #define EOF_NLEFT -99        /* value of parsenleft when EOF pushed back */
  56.  
  57.  
  58. /*
  59.  * The parsefile structure pointed to by the global variable parsefile
  60.  * contains information about the current file being read.
  61.  */
  62.  
  63. MKINIT
  64. struct parsefile {
  65.     int linno;        /* current line */
  66.     int fd;            /* file descriptor (or -1 if string) */
  67.     int nleft;        /* number of chars left in buffer */
  68.     char *nextc;        /* next char in buffer */
  69.     struct parsefile *prev;    /* preceding file on stack */
  70.     char *buf;        /* input buffer */
  71. };
  72.  
  73.  
  74. int plinno = 1;            /* input line number */
  75. MKINIT int parsenleft;        /* copy of parsefile->nleft */
  76. char *parsenextc;        /* copy of parsefile->nextc */
  77. MKINIT struct parsefile basepf;    /* top level input file */
  78. char basebuf[BUFSIZ];        /* buffer for top level input file */
  79. struct parsefile *parsefile = &basepf;    /* current input file */
  80. char *pushedstring;        /* copy of parsenextc when text pushed back */
  81. int pushednleft;        /* copy of parsenleft when text pushed back */
  82.  
  83. #ifdef __STDC__
  84. STATIC void pushfile(void);
  85. #else
  86. STATIC void pushfile();
  87. #endif
  88.  
  89.  
  90.  
  91. #ifdef mkinit
  92. INCLUDE "input.h"
  93. INCLUDE "error.h"
  94.  
  95. INIT {
  96.     extern char basebuf[];
  97.  
  98.     basepf.nextc = basepf.buf = basebuf;
  99. }
  100.  
  101. RESET {
  102.     if (exception != EXSHELLPROC)
  103.         parsenleft = 0;            /* clear input buffer */
  104.     popallfiles();
  105. }
  106.  
  107. SHELLPROC {
  108.     popallfiles();
  109. }
  110. #endif
  111.  
  112.  
  113. /*
  114.  * Read a line from the script.
  115.  */
  116.  
  117. char *
  118. pfgets(line, len)
  119.     char *line;
  120.     {
  121.     register char *p = line;
  122.     int nleft = len;
  123.     int c;
  124.  
  125.     while (--nleft > 0) {
  126.         c = pgetc_macro();
  127.         if (c == PEOF) {
  128.             if (p == line)
  129.                 return NULL;
  130.             break;
  131.         }
  132.         *p++ = c;
  133.         if (c == '\n')
  134.             break;
  135.     }
  136.     *p = '\0';
  137.     return line;
  138. }
  139.  
  140.  
  141.  
  142. /*
  143.  * Read a character from the script, returning PEOF on end of file.
  144.  * Nul characters in the input are silently discarded.
  145.  */
  146.  
  147. int
  148. pgetc() {
  149.     return pgetc_macro();
  150. }
  151.  
  152.  
  153. /*
  154.  * Refill the input buffer and return the next input character:
  155.  *
  156.  * 1) If a string was pushed back on the input, switch back to the regular
  157.  *    buffer.
  158.  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
  159.  *    from a string so we can't refill the buffer, return EOF.
  160.  * 3) Call read to read in the characters.
  161.  * 4) Delete all nul characters from the buffer.
  162.  */
  163.  
  164. int
  165. preadbuffer() {
  166.     register char *p, *q;
  167.     register int i;
  168.  
  169.     if (pushedstring) {
  170.         parsenextc = pushedstring;
  171.         pushedstring = NULL;
  172.         parsenleft = pushednleft;
  173.         if (--parsenleft >= 0)
  174.             return *parsenextc++;
  175.     }
  176.     if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
  177.         return PEOF;
  178.     flushout(&output);
  179.     flushout(&errout);
  180. retry:
  181.     p = parsenextc = parsefile->buf;
  182.     i = read(parsefile->fd, p, BUFSIZ);
  183.     if (i <= 0) {
  184. #ifndef __MINT__
  185.                 if (i < 0) {
  186. #endif
  187.                         if (errno == EINTR)
  188.                                 goto retry;
  189. #ifndef __MINT__
  190.                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
  191.                                 int flags = fcntl(0, F_GETFL, 0);
  192.                                 if (flags >= 0 && flags & O_NONBLOCK) {
  193.                                         flags &=~ O_NONBLOCK;
  194. #else
  195.                         if (parsefile->fd == 0 && i == 0) {
  196.                                 int flags = fcntl(0, F_GETFL, 0);
  197.                                 if (flags >= 0 && flags & O_NDELAY) {
  198.                                         flags &=~ O_NDELAY;
  199. #endif
  200.                                         if (fcntl(0, F_SETFL, flags) >= 0) {
  201.                         out2str("sh: turning off NDELAY mode\n");
  202.                                                 goto retry;
  203.                                         }
  204.                                 }
  205.                         }
  206. #ifndef __MINT__
  207.                 }
  208. #endif
  209.                 parsenleft = EOF_NLEFT;
  210.                 return PEOF;
  211.     }
  212.     parsenleft = i - 1;
  213.  
  214.     /* delete nul characters */
  215.     for (;;) {
  216. #ifdef __MINT__
  217.         if (*p == '\r') {
  218.             p++;
  219.             break;
  220.         }
  221. #endif
  222.         if (*p++ == '\0')
  223.             break;
  224.         if (--i <= 0)
  225.             return *parsenextc++;        /* no nul characters */
  226.     }
  227.     q = p - 1;
  228.     while (--i > 0) {
  229. #ifdef __MINT__
  230.         if (*p != '\0' && *p != '\r')
  231. #else
  232.         if (*p != '\0')
  233. #endif
  234.             *q++ = *p;
  235.         p++;
  236.     }
  237.     if (q == parsefile->buf)
  238.         goto retry;            /* buffer contained nothing but nuls */
  239.     parsenleft = q - parsefile->buf - 1;
  240.     return *parsenextc++;
  241. }
  242.  
  243.  
  244. /*
  245.  * Undo the last call to pgetc.  Only one character may be pushed back.
  246.  * PEOF may be pushed back.
  247.  */
  248.  
  249. void
  250. pungetc() {
  251.     parsenleft++;
  252.     parsenextc--;
  253. }
  254.  
  255.  
  256. /*
  257.  * Push a string back onto the input.  This code doesn't work if the user
  258.  * tries to push back more than one string at once.
  259.  */
  260.  
  261. void
  262. ppushback(string, length)
  263.     char *string;
  264.     {
  265.     pushedstring = parsenextc;
  266.     pushednleft = parsenleft;
  267.     parsenextc = string;
  268.     parsenleft = length;
  269. }
  270.  
  271.  
  272.  
  273. /*
  274.  * Set the input to take input from a file.  If push is set, push the
  275.  * old input onto the stack first.
  276.  */
  277.  
  278. void
  279. setinputfile(fname, push)
  280.     char *fname;
  281.     {
  282.     int fd;
  283.     int fd2;
  284.  
  285.     INTOFF;
  286.     if ((fd = open(fname, O_RDONLY)) < 0)
  287.         error("Can't open %s", fname);
  288.     if (fd < 10) {
  289.         fd2 = copyfd(fd, 10);
  290.         close(fd);
  291.         if (fd2 < 0)
  292.             error("Out of file descriptors");
  293.         fd = fd2;
  294.     }
  295.     setinputfd(fd, push);
  296.     INTON;
  297. }
  298.  
  299.  
  300. /*
  301.  * Like setinputfile, but takes an open file descriptor.  Call this with
  302.  * interrupts off.
  303.  */
  304.  
  305. void
  306. setinputfd(fd, push) {
  307.     if (push) {
  308.         pushfile();
  309.         parsefile->buf = ckmalloc(BUFSIZ);
  310.     }
  311.     if (parsefile->fd > 0)
  312.         close(parsefile->fd);
  313.     parsefile->fd = fd;
  314.     if (parsefile->buf == NULL)
  315.         parsefile->buf = ckmalloc(BUFSIZ);
  316.     parsenleft = 0;
  317.     plinno = 1;
  318. }
  319.  
  320.  
  321. /*
  322.  * Like setinputfile, but takes input from a string.
  323.  */
  324.  
  325. void
  326. setinputstring(string, push)
  327.     char *string;
  328.     {
  329.     INTOFF;
  330.     if (push)
  331.         pushfile();
  332.     parsenextc = string;
  333.     parsenleft = strlen(string);
  334.     parsefile->buf = NULL;
  335.     plinno = 1;
  336.     INTON;
  337. }
  338.  
  339.  
  340.  
  341. /*
  342.  * To handle the "." command, a stack of input files is used.  Pushfile
  343.  * adds a new entry to the stack and popfile restores the previous level.
  344.  */
  345.  
  346. STATIC void
  347. pushfile() {
  348.     struct parsefile *pf;
  349.  
  350.     parsefile->nleft = parsenleft;
  351.     parsefile->nextc = parsenextc;
  352.     parsefile->linno = plinno;
  353.     pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
  354.     pf->prev = parsefile;
  355.     pf->fd = -1;
  356.     parsefile = pf;
  357. }
  358.  
  359.  
  360. void
  361. popfile() {
  362.     struct parsefile *pf = parsefile;
  363.  
  364.     INTOFF;
  365.     if (pf->fd >= 0)
  366.         close(pf->fd);
  367.     if (pf->buf)
  368.         ckfree(pf->buf);
  369.     parsefile = pf->prev;
  370.     ckfree(pf);
  371.     parsenleft = parsefile->nleft;
  372.     parsenextc = parsefile->nextc;
  373.     plinno = parsefile->linno;
  374.     INTON;
  375. }
  376.  
  377.  
  378. /*
  379.  * Return to top level.
  380.  */
  381.  
  382. void
  383. popallfiles() {
  384.     while (parsefile != &basepf)
  385.         popfile();
  386. }
  387.  
  388.  
  389.  
  390. /*
  391.  * Close the file(s) that the shell is reading commands from.  Called
  392.  * after a fork is done.
  393.  */
  394.  
  395. void
  396. closescript() {
  397.     popallfiles();
  398.     if (parsefile->fd > 0) {
  399.         close(parsefile->fd);
  400.         parsefile->fd = 0;
  401.     }
  402. }
  403.